local M = _G.ServerOHOH.class.new(script,_G.ServerOHOH.files.Server_PlayerComponent(),script.Parent.Name);
local ServerOHOH = _G.ServerOHOH;
local ItemTool=_G.CommonOHOH.load.ItemTool()
local EventList = ServerOHOH.load.EventList();
local function_list = ServerOHOH.function_list;
local CarTool=_G.CommonOHOH.load.CarTool();
local Tool=require(script.tool)
function M.new(self,...)
    M(self,...);
end

function M.ctor(self,parent)
    M.super.ctor(self,parent)
    self.player = self.parent
    self.userId=self.parent:GetUserId()

    self.DataStore = self.parent:GetComponent(_G.ServerOHOH.load.Server_PlayerDataComponent());
    
    self.List = self:GetMatchData(self.DataStore:GetData("base").elo)
    self.PVPDataCtrl =  _G.server:GetComponent(_G.ServerOHOH.load.Server_PVPDataStoreCtrl())
      
    -- self.List = list
end
function M.GetMatchData(self,score)
   if  self.List then
    self:AddProcess(function()
        self.List = self.PVPDataStore:GetPvpPlayer(10)
	end);
    return  self.List
    
   else
  
    self.PVPDataStore = _G.server:GetComponent(_G.ServerOHOH.load.Server_PVPDataStoreCtrl()):GetPvpDataStoreByRankId(score)
    return self.PVPDataStore:GetPvpPlayer(10)
   end
  
--    print("服务器 PVPList", )
end

--本地玩家 （我） curPVPData =  {["PVPtime"] = 1730864953,
        --     ["isWin"] = true,
        --     ["reward"] = 100,
        --     ["score"] = 1001,
        --     ["userId"] = "2202500097"
--  } 
--参数 对手ID (rivalId)
function M.GenRecord(self,rivalId)

    --print("收到记录请求")
    local genTime = os.time()
    local exists = false
    local rivalScore = 0 
    -- 遍历 List 表
    for _, item in pairs(self.List) do
        if item["key"] == rivalId then
            exists = true
            rivalScore = item["value"]
            break
        end
    end
    if not  exists then
        --print("请求未通过",self.List)
        return
    end
   -- print("数据验证 List",self.List)
    
   
    if rivalId then
        local isWin = CarTool.IsPlayerWin(self.userId,rivalId)
        local myReward = Tool.GetReward(isWin,self.DataStore:GetData("base").elo)
        
        local curPVPData= {
            userId = rivalId,
            score =rivalScore,
            isWin = isWin,
            reward = myReward,
            PVPtime = genTime
        }

        print("cur" ,curPVPData)
		local PVPData =self.DataStore:GetData("PVPData").__real
        table.insert(PVPData,1, curPVPData)

        --上传PK后对手积分的更新状况
    --    local PVPDataCtrl =  _G.server:GetComponent(_G.ServerOHOH.load.Server_PVPDataStoreCtrl())
        self.PVPDataCtrl:AddPoint(curPVPData.userId,-curPVPData.reward)
       
        self.PVPDataCtrl:AddPvpData(curPVPData.userId,curPVPData,-curPVPData.reward)
        self.DataStore:GetData("base").elo = self.DataStore:GetData("base").elo + curPVPData.reward
        --规定最小为0
        if self.DataStore:GetData("base").elo < 0 then
            self.DataStore:GetData("base").elo =0
        end

        self.DataStore:Execut(function()
        end);
        self.parent:SynchDataToClient("PVPData")
        self.parent:SynchDataToClient("base")
        print("数据 userId",curPVPData)
        self.PVPDataCtrl:UpdatePlayerPvpPool(curPVPData.userId)
        self.PVPDataCtrl:UpdatePlayerPvpPool(self.userId)
    end
    
    return true
end

function M.GetPKResult(self,rivalId)

    local myCarData = {}
    local rivalCarData = {}

    self.PVPDataCtrl:GetPlayerCarInfo(self.userId,function(carData)
        -- print("myCarData ",carData["equip"])
       
        myCarData = { Tool.GetAllCarIdBySkinData(carData["skin"])}
    end)
    self.PVPDataCtrl:GetPlayerCarInfo(rivalId,function(carData)
        -- print("rivalCarData ",carData["equip"])
        rivalCarData = {Tool.GetAllCarIdBySkinData(carData["skin"])}
    end)
    local fail = true
    while fail do
       -- print("获取失败")
        task.wait(1);
        fail =  not next(myCarData) and not next(rivalCarData) 
    end;

    -- self.PVPDataCtrl:GetPlayerCarInfo(rivalId)
    local isWin = CarTool.IsPlayerWin(self.userId,rivalId)
    return isWin,myCarData,rivalCarData
end
function M.dtor(self)
    M.super.dtor(self)
end
--获取匹配数据
M:RegistFunction(function_list.get_match_data,M.GetMatchData)

--生成PK记录
M:RegistFunction(function_list.gen_record,M.GenRecord)

--获取PK结果
M:RegistFunction(function_list.get_pk_result,M.GetPKResult)

return M